package com.dudu.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;

import com.dudu.bean.ImsLogBean;
import com.dudu.bean.InterfaceLogBean;
import com.dudu.bean.SystemLogBean;
import com.dudu.dao.ImsLogDao;
import com.dudu.dao.InterfaceLogDao;
import com.dudu.dao.SystemLogDao;
import com.dudu.model.ImsLog;
import com.dudu.model.InterfaceLog;
import com.dudu.model.SystemLog;
import com.dudu.service.ILogService;
import com.dudu.timer.SavelogThread;
import com.dudu.utils.DateUtils;
import com.dudu.utils.DictionaryUtils;
import com.dudu.utils.ThreadPoolUtils;

import core.CoreDao;
import core.CoreDaoInvoke;

/**
 * 日志管理
 * 
 * @author gaoxs
 * 
 */
public class LogService implements ILogService {

    private CoreDao coreDao = CoreDaoInvoke.getInstance();

    private ImsLogDao imsLogDao = new ImsLogDao();
    private InterfaceLogDao interfaceLogDao = new InterfaceLogDao();
    private SystemLogDao logDao = new SystemLogDao();
    private String platformurl = DictionaryUtils.getValueByTypeAndKey("platform", "url");
    private ExecutorService executorService = ThreadPoolUtils.getThreadPoolUtils().getExecutorService();

    /**
     * 系统日志
     * 
     * @param type
     * @param userId
     * @param userName
     * @param tableName
     * @param context
     * @author gaoxs
     */
    public void log(String type, int userId, String uName, String tableName, String context) {
	try {

	    SystemLog log = new SystemLog();
	    log.setType(type);
	    log.setOperator_id(userId);
	    log.setOperator_name(uName);
	    log.setTable_name(tableName);
	    if(context.length()>3000){
		log.setContext(context.substring(0, 3000));
	    }else{
		 log.setContext(context);
	    }
	   
	    SavelogThread logthread = new SavelogThread(coreDao, log, logDao);
	    executorService.execute(logthread);
	} catch (Exception ex) {
	    //
	}
    }

    /**
     * 运营平台能力调用日志
     * 
     * @param during
     * @param url
     * @param request_msg
     * @param result
     */
    public void saveOperationPlatformLog(double during, String url, String request_msg, String result) {
	try {
	    if (url.startsWith(platformurl)) {
		String suburl = url.substring(url.lastIndexOf("/") + 1);
		String name = "";
		if (suburl.indexOf(".") > -1) {
		    name = suburl.substring(0, suburl.lastIndexOf("."));
		} else {
		    name = suburl;
		}
		ImsLog imsLog = new ImsLog();
		imsLog.setDuring(during);
		imsLog.setName(name);
		imsLog.setRequest_msg(request_msg.replace("{", "【").replace("}", "】"));

		if (StringUtils.isNotBlank(result)) {
		    JSONObject object = JSONObject.fromObject(result);
		    String result_status = object.optString("flag");
		    if (result.length() <= 2000) {
			imsLog.setResult_desc(result.replace("{", "【").replace("}", "】"));
		    } else {
			imsLog.setResult_desc(result.substring(0, 2000).replace("{", "【").replace("}", "】"));
		    }
		    if (StringUtils.isNotBlank(result_status) && result_status.equals("0000")) {
			imsLog.setResult_status("0");
		    } else {
			imsLog.setResult_status(result_status);
		    }
		} else {
		    imsLog.setResult_desc("");
		    imsLog.setResult_status("-1");
		}
		imsLog.setTime(DateUtils.getCurrentTime());
		imsLog.setType("Platform");
		imsLog.setPhoneNum("");
		SavelogThread logthread = new SavelogThread(coreDao, imsLog, imsLogDao);
		executorService.execute(logthread);
	    }
	} catch (Exception ex) {
	    ex.printStackTrace();
	}
    }

    @Override
    public void logIms(double during, String name, String request_msg, String result_desc, String result_status, String time, String type, String phoneNum) {
	try {
	    ImsLog imsLog = new ImsLog();
	    imsLog.setDuring(during);
	    // imsLog.setIdentify(identify);
	    imsLog.setName(name);
	    imsLog.setRequest_msg(request_msg);
	    if(result_desc.length()>2000){
		imsLog.setResult_desc(result_desc.substring(0,2000));
	    }else{
		imsLog.setResult_desc(result_desc);
	    }
	    
	    imsLog.setResult_status(result_status);
	    imsLog.setTime(time);
	    imsLog.setType(type);
	    imsLog.setPhoneNum(phoneNum);
	    SavelogThread logthread = new SavelogThread(coreDao, imsLog, imsLogDao);
	    executorService.execute(logthread);
	} catch (Exception ex) {
	    //
	}
    }

    @Override
    public void logInterface(double during, String name, String request_msg, String result_desc, String result_status, String time, String ip, String caller) {
	try {
	    InterfaceLog interfaceLog = new InterfaceLog();
	    interfaceLog.setDuring(during);
	    // interfaceLog.setIdentify(identify);
	    interfaceLog.setName(name);
	    if (request_msg.length() > 2000) {
		interfaceLog.setRequest_msg(request_msg.substring(0, 2000));
	    } else {
		interfaceLog.setRequest_msg(request_msg);
	    }
	    if (result_desc.length() > 2000) {
		interfaceLog.setResult_desc(result_desc.substring(0, 2000));
	    } else {
		interfaceLog.setResult_desc(result_desc);
	    }

	    interfaceLog.setResult_status(result_status);
	    interfaceLog.setTime(DateUtils.getFormatTimeNow());
	    interfaceLog.setIp(ip);
	    if (StringUtils.isNotBlank(caller)) {
		interfaceLog.setCaller(caller);
	    }
	    SavelogThread logthread = new SavelogThread(coreDao, interfaceLog, interfaceLogDao);
	    executorService.execute(logthread);

	} catch (Exception ex) {
	    //
	}
    }

    /**
     * 查询系统 日志总条数
     * 
     * @param paramSql
     * @return
     */
    public int getSysLogTotalAcount(String paramSql) {
	return logDao.getTotalAcount(coreDao, paramSql);
    }

    /**
     * 查询系统日志 记录分页
     * 
     * @param paramSql
     * @param pageSql
     * @return
     */
    public List<SystemLogBean> querySysLogAll(String paramSql, String pageSql) {
	List<SystemLogBean> result = new ArrayList<SystemLogBean>();
	List<SystemLog> list = logDao.queryAll(coreDao, paramSql, pageSql);
	if (null == list)
	    return result;
	for (SystemLog log : list) {
	    result.add(conversion(log));
	}
	return result;
    }

    /**
     * 第三方日志查询
     * 
     * @param paramSql
     * @param pageSql
     * @return
     */
    public List<ImsLogBean> queryImsLog(String paramSql, String pageSql) {
	List<ImsLogBean> result = new ArrayList<ImsLogBean>();
	List<ImsLog> list = imsLogDao.queryAll(coreDao, paramSql, pageSql);
	if (null == list)
	    return result;
	for (ImsLog log : list) {
	    result.add(conversion(log));
	}
	return result;
    }

    /**
     * 第三方调用日志总数
     * 
     * @param paramSql
     * @return
     */
    public int getImsLogTotalAcount(String paramSql) {
	return imsLogDao.getTotalAcount(coreDao, paramSql);
    }

    /**
     * 平台日志查询
     * 
     * @param paramSql
     * @param pageSql
     * @return
     */
    public List<InterfaceLogBean> queryInterfaceLog(String paramSql, String pageSql) {
	List<InterfaceLogBean> result = new ArrayList<InterfaceLogBean>();
	List<InterfaceLog> list = interfaceLogDao.queryAll(coreDao, paramSql, pageSql);
	if (null == list)
	    return result;
	for (InterfaceLog log : list) {
	    result.add(conversion(log));
	}
	return result;
    }

    /**
     * 能力平台调用日志总数
     * 
     * @param paramSql
     * @return
     */
    public int getInterfaceLogTotalAcount(String paramSql) {
	return interfaceLogDao.getTotalAcount(coreDao, paramSql);
    }

    /**
     * 转化
     * 
     * @param menu
     * @return
     */
    private InterfaceLogBean conversion(InterfaceLog bean) {
	InterfaceLogBean logBean = new InterfaceLogBean();
	BeanUtils.copyProperties(bean, logBean);// 源,目的
	// PropertyUtils.copyProperties(dest, orig);//目的,源
	return logBean;
    }

    /**
     * 转化
     * 
     * @param menuBean
     * @return
     */
    private InterfaceLog conversion(InterfaceLogBean bean) {
	InterfaceLog log = new InterfaceLog();
	BeanUtils.copyProperties(bean, log);// 源,目的
	// PropertyUtils.copyProperties(dest, orig);//目的,源
	return log;
    }

    /**
     * 转化
     * 
     * @param menu
     * @return
     */
    private ImsLogBean conversion(ImsLog bean) {
	ImsLogBean logBean = new ImsLogBean();
	BeanUtils.copyProperties(bean, logBean);// 源,目的
	// PropertyUtils.copyProperties(dest, orig);//目的,源
	return logBean;
    }

    /**
     * 转化
     * 
     * @param menuBean
     * @return
     */
    private ImsLog conversion(ImsLogBean bean) {
	ImsLog log = new ImsLog();
	BeanUtils.copyProperties(bean, log);// 源,目的
	// PropertyUtils.copyProperties(dest, orig);//目的,源
	return log;
    }

    /**
     * 转化
     * 
     * @param menu
     * @return
     */
    private SystemLogBean conversion(SystemLog bean) {
	SystemLogBean logBean = new SystemLogBean();
	BeanUtils.copyProperties(bean, logBean);// 源,目的
	// PropertyUtils.copyProperties(dest, orig);//目的,源
	return logBean;
    }

    /**
     * 转化
     * 
     * @param menuBean
     * @return
     */
    private SystemLog conversion(SystemLogBean bean) {
	SystemLog log = new SystemLog();
	BeanUtils.copyProperties(bean, log);// 源,目的
	// PropertyUtils.copyProperties(dest, orig);//目的,源
	return log;
    }

}
